package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.AssayCheckItem;
import com.xw.childhealth.service.AssayCheckItemService;
import com.xw.childhealth.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-21 09:39
 * @Version: 1.0
 */
@RequestMapping("/assayCheckItem")
@RestController
@Slf4j
public class AssayCheckItemController {


    @Autowired
    private AssayCheckItemService assayCheckItemService;


    @PostMapping("/getAssayCheckItem")
    public ResultVO getXqHospital() {
        return ResultVOUtil.success(recursionData(new ArrayList<>(), 0));
    }


    /**
     * 查找下级节点目录
     *
     * @param dataDictionaryVOList 数据字典 List
     * @param id                   查找的上级 id
     * @return
     */
    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, Integer id) {

        List<AssayCheckItem> parentNode = assayCheckItemService.findByParentIdEquals(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return dataDictionaryVOList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setId(e.getId());
            dataDictionaryVO.setTitle(e.getInformation());

            if (e.getInformation() != null) {
                log.error("  ");
                List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(list.size() == 0 ? null : list);

                dataDictionaryVOList.add(dataDictionaryVO);
            }

        });

        return dataDictionaryVOList;
    }


    /**
     * 修改或者添加
     *
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type,
                             @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "code", required = false) Integer code,
                             @RequestParam(value = "projectName", required = false) String projectName,
                             @RequestParam(value = "danwei", required = false) String danwei,
                             @RequestParam(value = "standardValue", required = false) String standardValue,
                             @RequestParam(value = "rangeOfValues", required = false) String rangeOfValues,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/assaycheckitem", map);
        }
        if (type.equals("toUpd")) {
            AssayCheckItem one = assayCheckItemService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            AssayCheckItem one = assayCheckItemService.findByIdEquals(nodeId);
            assayCheckItemService.save(one.setCode(code).setProjectName(projectName).setDanwei(danwei).setStandardValue(standardValue).setRangeOfValues(rangeOfValues).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/assaycheckitem", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            assayCheckItemService.save(new AssayCheckItem().setCode(code).setProjectName(projectName).setDanwei(danwei).setStandardValue(standardValue).setRangeOfValues(rangeOfValues).setUpdateTime(new Date()).setCreateTime(new Date()).setParentId(addRecordNode));
            return new ModelAndView("/pages/admin/assaycheckitem", map);
        }
        return new ModelAndView("/pages/admin/assaycheckitem-data-add", map);
    }


    /**
     * 查询父节点下面的子节点信息
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<AssayCheckItem> result = assayCheckItemService.findByParentIdEquals(nodeId);
        return ResultVOUtil.success(result);
    }

    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        AssayCheckItem result = assayCheckItemService.findByIdEquals(id);
        if (result != null) {
            assayCheckItemService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/assaycheckitem");
    }


}
